package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Cart;
import cn.wolfcode.trip.base.mapper.CartMapper;
import cn.wolfcode.trip.base.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author by mycp
 * @Classname CartServiceImpl
 * @Description TODO
 * @Date 2018/11/15 21:49
 */
@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<Cart> getCart(Long userId) {
        return cartMapper.getCartByUserId(userId);
    }

    @Override
    public void saveCart(Cart cart) {
        // 添加之前先判断用户是否有该购物车商品信息
        Cart existCart = cartMapper.getCartByUserIdAndProductId(cart);
        if (existCart == null) {
            // 添加该购物车信息
            cartMapper.insert(cart);
        } else {
            // 更新购物车信息
            existCart.setCount(cart.getCount());
            cartMapper.updateByPrimaryKey(existCart);
        }
    }

    @Override
    public void delete(Cart cart) {
        cartMapper.deleteCartByUserIdAndProductId(cart);
    }
}
